package newapp.dob.com.dc_new_app.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import newapp.dob.com.dc_new_app.R;
import newapp.dob.com.dc_new_app.adapter.PayWayAdapter;
import newapp.dob.com.dc_new_app.api.Api;
import newapp.dob.com.dc_new_app.api.bean.OrderListBean;
import newapp.dob.com.dc_new_app.util.RxLifeUtil;
import newapp.dob.com.dc_new_app.util.UI;
import newapp.dob.com.dc_new_app.view.OnLoadeMoreListener;

/**
 * Created by xsooy-pc on 2017/8/24.
 */

public class HomeFragment extends BaseFragment {
    @BindView(R.id.list)
    RecyclerView list;
    Unbinder unbinder;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.edit_search)
    EditText editSearch;
    @BindView(R.id.txt_empty)
    TextView txtEmpty;
    private OrderListBean bean = new OrderListBean();
    private PayWayAdapter adapter;
    private OnLoadeMoreListener onloadMoreListener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            loadData(page);
        }
    };

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        bean.pagesize = 15;
//        0,  // 订单状态：0 - 审核中、1 - 审核拒绝、2 - 审核成功
        bean.status = "0";
        list.addOnScrollListener(onloadMoreListener);
        adapter = new PayWayAdapter(getContext());
        list.setAdapter(adapter);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onloadMoreListener.setPage(1, 0, false);
                loadData(1);
            }
        });

        editSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int arg1, KeyEvent event) {
                if (arg1 == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    loadData(1);
                    return true;
                }
                return false;
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        loadData(1);
    }

    public void loadData(int page) {
        bean.pagenum = page;
        bean.keyword = UI.toString(editSearch);
        Api.api_service.getOrderList(bean).compose(RxLifeUtil.checkOn(this)).doOnComplete(() -> {
            if (refresh != null) {
                refresh.setRefreshing(false);
            }
        }).subscribe(result -> {
            if (result.data.page.total == 0) {
                txtEmpty.setVisibility(View.VISIBLE);
            }else {
                txtEmpty.setVisibility(View.GONE);
            }
            adapter.add(result.data.lists, bean.pagenum == 1);
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.btn_search)
    public void onViewClicked() {
        editSearch.clearFocus();
        onloadMoreListener.setPage(1, 0, false);
        loadData(1);
    }
}
